<template>
    <el-card>
        <div slot="header">
            <span>管理员信息</span>
        </div>

        <div class="text">
            <span>管理员ID: {{ id }}</span>
            <el-divider></el-divider>
            <span>账&emsp;号: {{ account }}</span>
            <el-divider></el-divider>
            <span>用户组: {{ userGroup }}</span>
            <el-divider></el-divider>
            <span>创建时间: {{ ctime | formater }}</span>
            <el-divider></el-divider>
            <span>管理员头像:</span>
            <el-upload class="avatar-uploader" action="http://api.wm.v6g.cn/users/avatar_upload"
                :on-success="success" :before-upload="beforeUpload" :show-file-list="false">
                <img v-if="imgUrl" :src="imgUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </div>
    </el-card>
</template>

<script>
import local from "@/utils/local.js";
import {avatarEdit,userInfo} from "@/api/user.js"
export default {
    data() {
        let userinfo = local.get("userinfo");
        return {
            imgUrl:"",
            id: userinfo.id,
            account: userinfo.account,
            userGroup: userinfo.userGroup,
            ctime: userinfo.ctime,
        };
    },
    methods: {
        // 上传成功的函数
        async success(res, file) {
            if (res.code === 0) {
                this.$message.success(res.msg);
                this.imgUrl = res.imgUrl;
            }
            // 将图片上传到数据库
            await avatarEdit({ imgUrl: res.imgUrl });
            // 传递参数
            this.$bus.$emit("sendImg", res.imgUrl);
        },
        // 上传之前执行的函数
        beforeUpload(file) {
            //MIME：文件类型的描述
            let mime = ["image/jpeg", "image/png", "image/gif"]; //允许上传三种格式的图片
            const isJPG = mime.includes(file.type);
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error("上传头像图片只能是 JPG、PNG、GIF 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传头像图片大小不能超过 2MB!");
            }
            return isJPG && isLt2M;
        },
    },
   async created(){
                // 获取管理员信息
                let res = await userInfo();
            this.imgUrl=res.data.imgUrl;

            console.log(res);
    }
};
</script>

<style>
.text {
    font-size: 16px;
    color: #666;
}

.avatar-uploader {
    display: inline-block;
    vertical-align: middle;
}

.el-upload--text {
    border: 1px dashed #f27b88;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>
